﻿using FanaticGene.IOExtension;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using ZetaIpc.Runtime.Client;

namespace FanaticGene.AvaloniaMaid.Caller
{
    internal class Program
    {
        [DllImport("user32.dll", EntryPoint = "SetForegroundWindow", SetLastError = true)]
        public static extern void SetForegroundWindow(IntPtr hwnd);


        static void Main(string[] args)
        {

            var avamaidName = "AvaMaid";
            var currentPath = AppDomain.CurrentDomain.BaseDirectory;
            var avamaidPath = PathEx.SeekFullFileName(currentPath, avamaidName + ".exe");

            var process = Process.GetProcessesByName(avamaidName).FirstOrDefault();

            if (process == null)
            {
                if (File.Exists(avamaidPath))
                {
                    Process.Start(avamaidPath, string.Join(" ", args));
                }
            }
            else
            {
                var c = new IpcClient();
                c.Initialize(7609);
                try
                {
                    c.Send(string.Join(",", args));
                }
                catch (Exception ex)
                {

                }


                SetForegroundWindow(process.MainWindowHandle);
            }


        }
    }
}
